/* eslint-disable */
new Vue({

    data : function() {
        return {

            query_info : {

                page : 1,

                per_page : 20,

                product_category_id : '',

                keyword : ''

            },                          

            search_info : {

                page : 1,

                per_page : 20,

            },                          //分页所需的搜索

            show_modal : false,      //弹框的显隐控制

            loading : false,            //loading的显隐控制

            active_tab : 'product',     //激活的tab的name

            product_active_tab : 'product_spec',    //商品tab的默认激活

            unique_opened : true,       //是否只能展开一个菜单

            init_categroy : '',         //初始选中的产品类别

            show_list : true,          //是否产品列表，true代表显示产品列表，false代表显示产品详情

            product_categories : [],    //产品分类列表

            product_detail : {},        //产品详情

            production : {},             //对应分类的产品信息

            last_position : '',         //记录浏览列表的scrollTop

        }
    },
    mounted : function() {

        this.fetchProductCategory()
            .then(this.hadParam)

    },
    computed: {
        
        selectedCategroyName : function selectedCategroyName(){

            var self = this;

            var selected_category_id = self.query_info.product_category_id;

            let selected_category_name = '';

            for (var i = 0; i < this.product_categories.length; i++) {

                var category = this.product_categories[i];

                if( category.get_sub_categories && category.get_sub_categories.length > 0 ) {

                    for (var j = 0; j < category.get_sub_categories.length; j++) {

                        var sub_category = category.get_sub_categories[j];

                        if( parseInt(sub_category.id) === parseInt(selected_category_id) ) {

                            selected_category_name = sub_category.name;

                            break;
    
                        }
                        
                    }

                }else if( parseInt(category.id) === parseInt(selected_category_id) ){

                    selected_category_name = category.name;

                }
                
            }

            return selected_category_name || '搜索结果';

        }

    },
    methods: {
        /**
         * 格式化网址上面的参数
         * @param  {string} url {需要进行格式化的网址}
         * @return {Object} {格式化后的参数对象}
         */
        fomatDataFromUrl : function fomatDataFromUrl(url){

            var new_arr = {};

            var param_arr = url.split('?');

            if( param_arr.length < 2 )return;

            param_arr.pop().split('&').forEach(function(param){

                new_arr[param.split('=')[0]] = param.split('=')[1];

            })

            return new_arr;

        },
        
        /**
         * 判断是否含有参数
         * @return {any}
         */
        hadParam : function hadParam(){

            var param = this.fomatDataFromUrl(location.href);

            if( !param ) {

                this.init_categroy = this.product_categories[0].get_sub_categories[0].id;

                this.selectMenu(this.product_categories[0].get_sub_categories[0].id);

                return;

            }

            var pid = param.pid;

            var detail_id = param.detail_id;

            var keyword = param.keyword;

            if( keyword ) {

                this.query_info.keyword = decodeURI(keyword);

                this.fetchProductList(this.query_info)
                    .then(function(){

                        if( !detail_id ) return;

                        this.watchProductDetail(detail_id);

                    })

                return;

            }

            this.init_categroy = pid;

            this.selectMenu(pid)
                .then(function(){

                    if( !detail_id ) return;

                    this.watchProductDetail(detail_id);

                })

        },

        /**
         * 下载资料
         * @return {undefined}
         */
        downloadInformation : function downloadInformation(){

            window.location.href = this.product_detail.information;

        },

        /**
         * 选中菜单的回调
         * @param  {string} index {选中的菜单的index值}
         * @return {undefined}
         */
        selectMenu : function selectMenu(index){

            const url = 'production?pid=' + index;

            history.replaceState({},'',url);

            this.show_list = true;

            this.query_info.product_category_id = index;

            this.query_info.keyword = '';

            this.search_info = JSON.parse(JSON.stringify(this.query_info));

            window.scrollTo( 0 , 450 );

            return this.fetchProductList(this.query_info);

        },

        /**
         * 返回产品列表
         * @return {undefined}
         */
        returnList : function returnList(){

            if( this.query_info.product_category_id ) {

                history.replaceState({},'','production?pid=' + this.query_info.product_category_id);

            }

            if( this.query_info.keyword ) {

                history.replaceState({},'','production?keyword=' + this.query_info.keyword);

            }

            this.show_list = true;

            window.scrollTo( 0 , this.last_position );

        },

        /**
         * 查看产品详情
         * @param  {string} id {产品的id}
         * @return {undefined}
         */
        watchProductDetail : function watchProductDetail(id){

            if( this.query_info.product_category_id ) {

                history.replaceState({},'','production?pid=' + this.query_info.product_category_id + '&detail_id=' + id)

            }
            
            if( this.query_info.keyword ) {

                history.replaceState({},'','production?keyword=' + this.query_info.keyword + '&detail_id=' + id)

            }

            this.show_list = false;

            this.product_detail = this.production.data.filter(function(product){return parseInt(product.id) === parseInt(id)})[0];

            this.last_position = document.body.scrollTop;

            window.scrollTo( 0 , 450 );

        },

        /**
         * 每页显示数改变的回调
         * @param  {string} size {改变的每页显示数}
         * @return {undefined}
         */
        handleSizeChange : function handleSizeChange(size){

            this.query_info.per_page = size;

            this.search_info.per_page = size;

            this.fetchProductList(this.search_info);

        },

        /**
         * 页码改变时的回调
         * @param  {string} currentPage {改变的页码}
         * @return {undefined}
         */
        handleCurrentChange : function handleCurrentChange(currentPage){

            this.search_info.page = currentPage;

            console.log(this.query_info);

            this.fetchProductList(this.search_info);            

        },

        /**
         * 获取产品分类
         * @return {promise}
         */
        fetchProductCategory : function fetchProductCategory(){

            return this.$http.get('/api/home/production/create')
                            .then(function(res){
                                        
                                if( parseInt(res.data.code) !== 0 )throw new Error(res.data.error);

                                this.product_categories = res.data.product_categories;
                                        
                            })
                            .catch(function(err){
                        
                                console.log( err.message );

                            })

        },

        /**
         * 获取产品列表
         * @param  {Object} param {搜索产品列表的参数}
         * @return {undefined}
         */
        fetchProductList : function fetchProductList(param){

            this.loading = true;

            var query_str = Qs.stringify(param);

            return this.$http.get( '/api/home/production?' + query_str )
                            .then(function(res){
                                        
                                if( parseInt(res.data.code) !== 0 )throw new Error(res.data.error);

                                this.production = res.data.production;
                                        
                            })
                            .catch(function(err){
                                console.log(err);
                            })
                            .then(function(){
                                this.loading = false;
                            })

        }

    }

}).$mount('#production')